package com.pz.cloud.framework.business.workflow.aspect;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pz.cloud.framework.base.model.WorkflowEntity;
import com.pz.cloud.framework.business.workflow.entity.WaitDoneUser;
import com.pz.cloud.framework.business.workflow.service.IWaitDoneService;
import com.pz.cloud.framework.util.BizUtil;
import com.pz.cloud.framework.util.WorkflowUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Aspect
@Order(1)
@Component
public class AssembleDealFlagAspect {

    @Autowired
    private IWaitDoneService waitDoneService;

    @Around("@annotation(com.pz.cloud.framework.business.workflow.annotation.AssembleDealFlag)")
    public Object submit(ProceedingJoinPoint joinPoint) throws Throwable{
        Object result = null;
        result = joinPoint.proceed();

        if(result instanceof WorkflowEntity){
            WorkflowUtil.assembleDealFlag((WorkflowEntity) result);
        }else if(result instanceof Result){
            Object r = ((Result) result).getResult();
            if(r instanceof  WorkflowEntity){
                WorkflowUtil.assembleDealFlag((WorkflowEntity) r);
            }else if(r instanceof IPage){
                List records = ((IPage) r).getRecords();
                if(records!=null){
                    for (Object record : records) {
                        if(record instanceof WorkflowEntity){
                            WorkflowUtil.assembleDealFlag((WorkflowEntity) record);
                        }else if(record instanceof JSONObject){
                            String id = (String) ((JSONObject) record).get("id");
                            if(id!=null){
                                Integer dealFlag = WorkflowUtil.getDealFlagByBizId(id);
                                ((JSONObject) record).put("dealFlag",dealFlag);
                                List<WaitDoneUser> waitDoneUser = waitDoneService.getCurUserByBizId(id);
                                if(!CollectionUtils.isEmpty(waitDoneUser)){
                                    String waitUserName = waitDoneUser.stream().map(WaitDoneUser::getWaitUserName).collect(Collectors.joining(","));
                                    ((JSONObject) record).put("waitHandler",waitUserName);
                                }
                                WorkflowEntity workflowEntity = ((JSONObject) record).toJavaObject(WorkflowEntity.class);

                                Integer actStatus = workflowEntity.getActStatus();
                                if(actStatus!=null&&actStatus <10&& actStatus !=0&&BizUtil.getUserId().equals(workflowEntity.getCreatorId())){
                                    //标记是否可以撤销流程
                                    ((JSONObject) record).put("cancelFlag",1);
                                }
                            }

                        }
                    }
                }
            }
        }
        return result;
    }
}